<template>
  <div class="search" v-loading="loading">
    <div class="shop-list">
      <div class="shop-item" v-for="(item, index) in shopList" :key="index">
        <div class="shop-info">
          <img :src="item.shop_img" alt="" />
          <div class="infos">
            <div class="title">
              {{ item.shop_name }}
              <el-button
                @click="setShopFollow(item)"
                class="btn"
                type="primary"
                v-if="!item.shop_follow"
                >收藏店铺</el-button
              >
              <el-button @click="setShopFollow(item)" class="btn" v-else
                >取消收藏</el-button
              >
            </div>
            <div class="attention">{{ item.shop_collection }}人关注</div>
            <div class="val">
              <div class="item">
                <span>所有商品</span> <b>{{ item.shop_goods }}</b>
              </div>
              <div class="item">
                <span>新品</span> <b>{{ item.shop_new }}</b>
              </div>
              <div class="item">
                <span>促销</span> <b>{{ item.shop_promotion }}</b>
              </div>
            </div>
          </div>
        </div>
        <div class="goods-list">
          <div
            class="item"
            @click="toGoodsPage(item.id)"
            v-for="(item, index) in item.good"
            :key="index"
          >
            <img :src="item.banner[0].url" alt="" />
            <div class="title">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
    <center class="page-box">
      <Pages1
        @change="getShopsList"
        :total="false"
        :pages="false"
        :jump="false"
        v-model="query.current"
        :size="query.length"
        :count="query.total"
      />
    </center>
  </div>
</template>

<script>
import api from "@/api";
import Pages1 from "@/components/Pages/Pages1.vue";
export default {
  name: "Search",
  components: { Pages1 },
  data() {
    return {
      loading: false,
      query: {
        search: "",
        current: 1,
        length: 10,
        total: 0,
      },
      shopList: [],
    };
  },
  watch: {
    "$route.query.search"() {
      this.getShopsList();
    },
  },
  created() {
    this.getShopsList();
  },
  methods: {
    async getShopsList() {
      this.loading = true;
      this.query.search = this.$route.query.search;
      let result = await api.SHOP_LIST(this.query);
      this.query = Object.assign({}, result, { data: null });
      this.shopList = result.data;
      console.log(this.shopList);
      this.loading = false;
      window.scrollTo(0, 0);
    },
    async setShopFollow(item) {
      await api.SHOPS_COLLECTION({
        shop_id: item.shop_id,
        status: item.shop_follow ? 0 : 1,
      });
      Object.assign(item, { shop_follow: item.shop_follow ? 0 : 1 });
      this.$message.success("操作成功");
    },
  },
};
</script>

<style lang="scss" scoped>
.search {
  margin-bottom: 20px;
  .shop-list {
    min-height: 600px;
  }
  .shop-item {
    max-width: 1200px;
    margin: 20px auto 0;
    box-sizing: border-box;
    height: 204px;
    background: #ffffff;
    border-radius: 5px;
    border: 1px solid #f0f0f0;
    padding: 20px;
    display: flex;
    .shop-info {
      display: flex;
      flex: 1;
      img {
        width: 94px;
        height: 94px;
        border-radius: 2px;
        margin-right: 20px;
      }
      .infos {
        flex: 1;
        width: 0;
        margin-right: 10px;
        .title {
          font-size: 18px;
          font-weight: 600;
          color: #333333;
          line-height: 25px;
          display: flex;
          align-items: center;
          .btn {
            margin-left: 20px;
            width: 120px;
            height: 28px;
            border-radius: 5px;
            font-size: 14px;
            display: flex;
            justify-content: center;
            align-items: center;
          }
        }
        .attention {
          font-size: 16px;
          font-weight: 400;
          color: #999999;
          margin: 12px 0;
        }
        .val {
          display: flex;
          align-items: center;
          font-size: 16px;
          color: #666666;
          line-height: 22px;
          margin-right: 20px;
          b {
            color: #dc2c1a;
            margin: 0 5px;
            font-weight: 400;
          }
        }
      }
    }
    .goods-list {
      display: flex;
      & .item:first-child {
        margin-left: 0;
      }
      .item {
        cursor: pointer;
        width: 120px;
        margin-left: 40px;
        img {
          width: 120px;
          height: 120px;
          border-radius: 5px;
        }
        .title {
          margin-top: 10px;
          font-size: 12px;
          color: #333333;
          line-height: 17px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
      }
    }
  }
  .page-box {
    margin-top: 20px;
  }
}
</style>
